<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var str = '你好，{{name}},欢迎来到{{ company }}';
        var info = { name: '张三', company: '科技云' };
        // 要得到结果: '你好,张三,欢迎来到科技云'
        // 方法一：
        // var pattern = /{{\s*([\w]+)\s*}}/;
        // var patternResult = null;
        // while (patternResult = pattern.exec(str)) {
        //     str = str.replace(patternResult[0], info[patternResult[1]])
        // }
        // 方法二：
        str = str.replace(/{{\s*([\w]+)\s*}}/g, (str2, key) => info[key])
        str = str.replace(pattern, '$1')
        // 打印结果
        console.log(str);
    </script>
</body>

</html>